package com.longxi.tianjintianbao.ui.presenter.repair_publish;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;

import com.alibaba.fastjson.JSON;
import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.model.RepairType;
import com.longxi.tianjintianbao.ui.activity.repair_publish.RepairContentActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.LabelNoBackgroundViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.RepairTypeViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.repair.IRepairAtView;
import com.longxi.tianjintianbao.utils.GapFillUtils;
import com.longxi.tianjintianbao.utils.JsonReadUtils;
import com.longxi.tianjintianbao.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class RepairAtPresenter extends BasePresenter<IRepairAtView> {

    private MultiTypeAdapter adapter;
    private Items items;

    String regionJson = "";//json
    String region = "";//维修区域
    int SPAN_COUNT = 4;
    GapFillUtils<RepairType> gapFillUtils = new GapFillUtils<>();
    List<RepairType.Area> mList = new ArrayList<>();

    public RepairAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        regionJson =intent.getStringExtra(AppConst.REPAIRAREA);
        String oaJson = JsonReadUtils.getJsonStr(mContext, regionJson);
        mList = JSON.parseArray(oaJson, RepairType.Area.class);
        region =intent.getStringExtra(AppConst.REGION);
        if("0".equals(region)){
            mContext.setTitle("室内维修");
        }else{
            mContext.setTitle("公共维修");
        }

        mContext.setTvLeft(MyUtils.getString(R.string.home));
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(String.class, new LabelNoBackgroundViewBinder());
            adapter.register(RepairType.Type.class, new RepairTypeViewBinder().setListener(new RepairTypeViewBinder.OnClickListener() {
                @Override
                public void onClick(int position) {
                    RepairType.Type type = (RepairType.Type) items.get(position);
                    if(!mContext.getIntent().getBooleanExtra(AppConst.STATE, false)){
                        Intent intent = new Intent(mContext, RepairContentActivity.class);
                        intent.putExtra(AppConst.REGION,region);
                        intent.putExtra(AppConst.REPAIRTYPE,type.repairtype);
                        intent.putExtra(AppConst.REPAIRCATEGORY,type.repaircategory);
                        mContext.startActivity(intent);
                    }else{
                        Intent intent = mContext.getIntent();
                        Bundle bundle = intent.getExtras();
                        bundle.putString(AppConst.REGION,region);
                        bundle.putString(AppConst.REPAIRTYPE,type.repairtype);
                        bundle.putString(AppConst.REPAIRCATEGORY,type.repaircategory);
                        intent.putExtras(bundle);
                        mContext.setResult(mContext.RESULT_OK, intent);
                        mContext.finish();
                    }
                }
            }));
            final GridLayoutManager layoutManager = new GridLayoutManager(mContext, SPAN_COUNT);
            GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    Object item = items.get(position);
                    if (item instanceof String) {
                        return 4;
                    }
                    if (item instanceof RepairType.Type) {
                        return 1;
                    }
                    return SPAN_COUNT;
                }
            };
            layoutManager.setSpanSizeLookup(spanSizeLookup);
            getView().getRvContent().setLayoutManager(layoutManager);
            getView().getRvContent().setAdapter(adapter);

        }
    }

    public void getRepairType(){
        items = new Items();
        for (RepairType.Area area : mList) {
            items.add(area.repairtype);
            items.addAll(area.item);
        }
        adapter.setItems(items);
        adapter.notifyDataSetChanged();
        mContext.hideLoading();
//        JSONObject object = new JSONObject();
//        try {
//            object.put("region", region);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//
//        ApiRetrofit.getInstance().getRegionType(object)
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//        .subscribe(new MySubscriber<HttpResult<RepairType>>(mContext) {
//            @Override
//            public void onError(ExceptionHandle.ResponeThrowable throwable) {
//                mContext.showNoData();
//            }
//
//            @Override
//            public void onNext(HttpResult<RepairType> repairResponse) {
//                items = new Items();
//                if(repairResponse.getData() == null){
//                    mContext.showNoData();
//                    return;
//                }
//                RepairType other = new RepairType();
//                other.setRegionname("其他");
//                repairResponse.getData().add(other);
//                items.addAll(gapFillUtils.add(repairResponse.getData(), new RepairType(), SPAN_COUNT));
//                adapter.setItems(items);
//                adapter.notifyDataSetChanged();
//                mContext.hideLoading();
//            }
//        });
    }

    @Override
    public void detachView() {
        super.detachView();
        this.adapter = null;
        this.items = null;

        this.mList = null;
    }
}
